import {
  ErrorCode
} from '@/utils/ccplugin/NPCCErrorCode'
import {
  NPCCIF
} from '@/utils/ccplugin/NPCCInterface'
import {
  NPCCILY
} from '@/utils/ccplugin/NPCCInterlayer'

//获取当前需要的资源
export function getSubRes(devHandle, resourceType) {
  var operator = NPCCIF.COMMON_GetChildren(NPCCILY.Plug.ccPlugin, devHandle);
  if (operator.errorCode == ErrorCode.NP_E_OK && operator.value) {
    if (operator.value.constructor != Array) {
      operator.value = [operator.value];
    }

    var getHandle = null;
    // $.each(operator.value, function(resIdx, resHandle) {
    //     var resType = NPCCIF.COMMON_GetResType(NPCCILY.Plug.ccPlugin, resHandle).value;
    //     if (Number(resType) == resourceType) {
    //         getHandle = resHandle;
    //         return false;
    //     }
    // })
    operator.value.map((resHandle, resIdx) => {
      var resType = NPCCIF.COMMON_GetResType(NPCCILY.Plug.ccPlugin, resHandle).value;
      if (Number(resType) == resourceType) {
        getHandle = resHandle;
        return false;
      }
    })

    return getHandle;
  }
}

// 资源树加载时获取每个设备对应的所有摄像头句柄
export function getSubResCameraHandle(devHandle, resourceType) {
  var operator = NPCCIF.COMMON_GetChildren(NPCCILY.Plug.ccPlugin, devHandle);
  if (operator.errorCode == ErrorCode.NP_E_OK && operator.value) {
    if (operator.value.constructor != Array) {
      operator.value = [operator.value];
    }

    var getHandle = [];
    // $.each(operator.value, function(resIdx, resHandle) {
    // var resType = NPCCIF.COMMON_GetResType(NPCCILY.Plug.ccPlugin, resHandle).value;
    // if (Number(resType) == resourceType) {
    //     getHandle.push(resHandle);
    // }
    // })
    operator.value.map((resHandle, resIdx) => {
      var resType = NPCCIF.COMMON_GetResType(NPCCILY.Plug.ccPlugin, resHandle).value;
      if (Number(resType) == resourceType) {
        getHandle.push(resHandle);
      }
    })

    return getHandle;
  }
}

// 获取兄弟资源
export function getSiblingRes(cameraHandle, resourceType) {
  var devHandle = NPCCIF.COMMON_GetParent(NPCCILY.Plug.ccPlugin, cameraHandle).value;
  var operator = NPCCIF.COMMON_GetChildren(NPCCILY.Plug.ccPlugin, devHandle);
  if (operator.errorCode == ErrorCode.NP_E_OK && operator.value) {
    if (operator.value.constructor != Array) {
      operator.value = [operator.value];
    }

    var getHandle = null;
    operator.value.map((resHandle, resIdx) => {
      var resType = NPCCIF.COMMON_GetResType(NPCCILY.Plug.ccPlugin, resHandle).value;
      if (Number(resType) == resourceType) {
        getHandle = resHandle;
        return false;
      }
    })
    // $.each(operator.value, function(resIdx, resHandle) {
    // var resType = NPCCIF.COMMON_GetResType(NPCCILY.Plug.ccPlugin, resHandle).value;
    // if (Number(resType) == resourceType) {
    //     getHandle = resHandle;
    //     return false;
    // }
    // })

    return getHandle;
  }
}

// 获取当前在线设备句柄
export function onlineDevice() {
  var onlines = [];
  //获取所有设备句柄
  var operator = NPCCIF.RSTW_GetPUList(NPCCILY.Plug.ccPlugin, WebClient.hServer);
  if (operator.errorCode == ErrorCode.NP_E_OK) {
    var hPuArray = operator.value;
    // 进行在线排序
    hPuArray.value.map((devHandle, index) => {
      var bOnline = NPCCIF.COMMON_IsOnline(NPCCILY.Plug.ccPlugin, devHandle).value;
      var bEnable = NPCCIF.COMMON_IsEnable(NPCCILY.Plug.ccPlugin, devHandle).value;
      if (bOnline == 1 && bEnable == 1) {
        onlines.push(devHandle);
      }
    })
    // $.each(hPuArray, function(index, devHandle) {
    // var bOnline = NPCCIF.COMMON_IsOnline(NPCCILY.Plug.ccPlugin, devHandle).value;
    // var bEnable = NPCCIF.COMMON_IsEnable(NPCCILY.Plug.ccPlugin, devHandle).value;
    // if (bOnline == 1 && bEnable == 1) {
    //     onlines.push(devHandle);
    // }
    // });
  }

  return onlines;
}